import json
import re

import requests

# headers = {
#     'Content-Type': 'text/plain',
#     'Cookie': 'lang=en-US; JSESSIONID_BOUTIQUE=0195585D42F517632BFE0EF8D886C651;'
# }
# url = 'https://www.infogreffe.com/services/entreprise/rest/recherche/resumeEntreprise?typeRecherche=ENTREP_RCS_ACTIF'
# resp = requests.post(url,headers=headers,data='[13612338,14559791]').content.decode()
# print(resp)

content = '''{"success":true,"identifier":"id","label":"deno","loadedAttr":"numeroDossier","idRecherche":null,"nbTotalResultats":0,"items":[{"id":12188321,"numeroDossier":"120306D00085","etablissementChrono":"0000","libelleEntreprise":{"denomination":"EXPLOITATION AGRICOLE A RESPONSABILITE LIMITEE AUREJAC","denominationEirl":null,"enseigne":null,"nomCommercial":null,"sigle":null},"siren":489032375,"nic":"00011","adresse":{"lignes":["MIQUELS "],"codePostal":"12240","bureauDistributeur":"RIEUPEYROUX"},"codePaysRegistreEtranger":null,"greffe":{"numero":"1203","nom":"RODEZ","codeGroupement":"03","codeEDI":"G1203","typeTribunalReel":"TC","nomGreffeMin":null,"codeLiasseDGFIP":null},"typeEtab":"SIE","produitAuPanier":"AJOUTABLE","typeInscription":1,"sourceDonnees":"GTC","radie":false,"dateRadiation":null,"nbEtablissements":1,"activite":{"codeNAF":"0142Z","libelleNAF":"Élevage d'autres bovins et de buffles"},"etatSurveillance":"SURVEILLABLE"},{"id":14618928,"numeroDossier":"330289B01643","etablissementChrono":"0000","libelleEntreprise":{"denomination":"SOCIETE A RESPONSABILITE LIMITEE DE LA PRESQU'ILE","denominationEirl":null,"enseigne":null,"nomCommercial":"SARL DE LA PRESQU'ILE","sigle":null},"siren":351498993,"nic":"00010","adresse":{"lignes":["LIEU-DIT CLAOUEY","18 AVENUE GÉNÉRAL DE GAULLE "],"codePostal":"33950","bureauDistributeur":"LÈGE-CAP-FERRET"},"codePaysRegistreEtranger":null,"greffe":{"numero":"3302","nom":"BORDEAUX","codeGroupement":"03","codeEDI":"G3302","typeTribunalReel":"TC","nomGreffeMin":null,"codeLiasseDGFIP":null},"typeEtab":"SIE","produitAuPanier":"AJOUTABLE","typeInscription":1,"sourceDonnees":"GTC","radie":false,"dateRadiation":null,"nbEtablissements":2,"activite":{"codeNAF":"6831Z","libelleNAF":"Agences immobilières"},"etatSurveillance":"SURVEILLABLE"},{"id":4950125,"numeroDossier":"940186B12804","etablissementChrono":"0000","libelleEntreprise":{"denomination":"COMPLEXE D ENSEIGNEMENT DE LA CONDUITE DE VEHICULES A MOTEUR","denominationEirl":null,"enseigne":null,"nomCommercial":null,"sigle":null},"siren":300511011,"nic":null,"adresse":{"lignes":["206 QUA. AV. DIV. LECLERC "],"codePostal":"94460","bureauDistributeur":"VALENTON"},"codePaysRegistreEtranger":null,"greffe":{"numero":"9401","nom":"CRETEIL","codeGroupement":"03","codeEDI":"G9401","typeTribunalReel":"TC","nomGreffeMin":null,"codeLiasseDGFIP":null},"typeEtab":"SIE","produitAuPanier":"AJOUTABLE","typeInscription":1,"sourceDonnees":"GTC","radie":false,"dateRadiation":null,"nbEtablissements":0,"activite":{"codeNAF":null,"libelleNAF":null},"etatSurveillance":"SURVEILLABLE"},{"id":6646094,"numeroDossier":"675260A01530","etablissementChrono":"0000","libelleEntreprise":{"denomination":"SPIELMANN Anne","denominationEirl":null,"enseigne":"ENTREPRISE DE CONSTRUCTION A. KIEHL & FILS","nomCommercial":"A. KIEHL","sigle":null},"siren":null,"nic":null,"adresse":{"lignes":["6 RUE DU MAENNELSTEIN","STRASBOURG NEUDORF"],"codePostal":"67100","bureauDistributeur":"STRASBOURG"},"codePaysRegistreEtranger":null,"greffe":{"numero":"6752","nom":"STRASBOURG","codeGroupement":"05","codeEDI":"G6702","typeTribunalReel":"TI","nomGreffeMin":null,"codeLiasseDGFIP":null},"typeEtab":"PRI","produitAuPanier":"AJOUTABLE","typeInscription":1,"sourceDonnees":"GTC","radie":false,"dateRadiation":null,"nbEtablissements":0,"activite":{"codeNAF":null,"libelleNAF":null},"etatSurveillance":"SURVEILLABLE"},{"id":13838799,"numeroDossier":"310221A04893","etablissementChrono":"0000","libelleEntreprise":{"denomination":"VALES Maria Amélia","denominationEirl":null,"enseigne":null,"nomCommercial":"LES POTES A PATTES","sigle":null},"siren":902696723,"nic":"00011","adresse":{"lignes":["41 RUE DU PRÉ VICINAL "],"codePostal":"31270","bureauDistributeur":"CUGNAUX"},"codePaysRegistreEtranger":null,"greffe":{"numero":"3102","nom":"TOULOUSE","codeGroupement":"51","codeEDI":"G3102","typeTribunalReel":"TC","nomGreffeMin":null,"codeLiasseDGFIP":null},"typeEtab":"PRI","produitAuPanier":"AJOUTABLE","typeInscription":1,"sourceDonnees":"GTC","radie":false,"dateRadiation":null,"nbEtablissements":1,"activite":{"codeNAF":"9609Z","libelleNAF":"Autres services personnels n.c.a."},"etatSurveillance":"SURVEILLABLE"},{"id":15144940,"numeroDossier":"720298D00395","etablissementChrono":"0000","libelleEntreprise":{"denomination":"EXPLOITATION AGRICOLE A RESPONSABILITE LIMITE BELIARD","denominationEirl":null,"enseigne":null,"nomCommercial":null,"sigle":"EARL BELIARD"},"siren":421276460,"nic":"00019","adresse":{"lignes":["LE BOIS DE BRICE "],"codePostal":"72350","bureauDistributeur":"POILLE-SUR-VEGRE"},"codePaysRegistreEtranger":null,"greffe":{"numero":"7202","nom":"LE MANS","codeGroupement":"03","codeEDI":"G7202","typeTribunalReel":"TC","nomGreffeMin":null,"codeLiasseDGFIP":null},"typeEtab":"SIE","produitAuPanier":"AJOUTABLE","typeInscription":1,"sourceDonnees":"GTC","radie":false,"dateRadiation":null,"nbEtablissements":1,"activite":{"codeNAF":"0141Z","libelleNAF":"Élevage de vaches laitières"},"etatSurveillance":"SURVEILLABLE"},{"id":8644991,"numeroDossier":"590613D00356","etablissementChrono":"0000","libelleEntreprise":{"denomination":"EXPLOITATION AGRICOLE A RESPONSABILITE LIMITEE DE LA FERME DU PARADIS","denominationEirl":null,"enseigne":null,"nomCommercial":null,"sigle":null},"siren":798638771,"nic":"00013","adresse":{"lignes":["13 RUE DU CROMBOULIE"],"codePostal":"59740","bureauDistributeur":"CLAIRFAYTS"},"codePaysRegistreEtranger":null,"greffe":{"numero":"5906","nom":"VALENCIENNES","codeGroupement":"02","codeEDI":"G5909","typeTribunalReel":"TC","nomGreffeMin":null,"codeLiasseDGFIP":null},"typeEtab":"SIE","produitAuPanier":"AJOUTABLE","typeInscription":1,"sourceDonnees":"GTC","radie":false,"dateRadiation":null,"nbEtablissements":1,"activite":{"codeNAF":"0145Z","libelleNAF":"Élevage d'ovins et de caprins"},"etatSurveillance":"SURVEILLABLE"},{"id":15182961,"numeroDossier":"750102B02976","etablissementChrono":"0000","libelleEntreprise":{"denomination":"BLANQUI DEPANNAGES A DOMICILE","denominationEirl":null,"enseigne":"SOS DEPANNAGE - OK SERVICE","nomCommercial":"SOS DEPANNAGE - OK SERVICE","sigle":null},"siren":440927614,"nic":"00014","adresse":{"lignes":["66 BOULEVARD AUGUSTE BLANQUI"],"codePostal":"75013","bureauDistributeur":"PARIS"},"codePaysRegistreEtranger":null,"greffe":{"numero":"7501","nom":"PARIS","codeGroupement":"01","codeEDI":"G7501","typeTribunalReel":"TC","nomGreffeMin":null,"codeLiasseDGFIP":null},"typeEtab":"SIE","produitAuPanier":"AJOUTABLE","typeInscription":1,"sourceDonnees":"GTC","radie":false,"dateRadiation":null,"nbEtablissements":1,"activite":{"codeNAF":"9522Z","libelleNAF":"Réparation d'app. électroménagers et d'équipements pour la maison et le jardin"},"etatSurveillance":"SURVEILLABLE"},{"id":13799035,"numeroDossier":"810221B00452","etablissementChrono":"0000","libelleEntreprise":{"denomination":"Du pré à la bouche","denominationEirl":null,"enseigne":null,"nomCommercial":null,"sigle":"DPB"},"siren":902253137,"nic":"00019","adresse":{"lignes":["LIEU-DIT VIALELLES"],"codePostal":"81330","bureauDistributeur":"SAINT-PIERRE-DE-TRIVISY"},"codePaysRegistreEtranger":null,"greffe":{"numero":"8102","nom":"CASTRES","codeGroupement":"03","codeEDI":"G8102","typeTribunalReel":"TC","nomGreffeMin":null,"codeLiasseDGFIP":null},"typeEtab":"SIE","produitAuPanier":"AJOUTABLE","typeInscription":1,"sourceDonnees":"GTC","radie":false,"dateRadiation":null,"nbEtablissements":1,"activite":{"codeNAF":"4781Z","libelleNAF":"Commerce de détail alimentaire sur éventaires et marchés"},"etatSurveillance":"SURVEILLABLE"},{"id":10770866,"numeroDossier":"310208B00454","etablissementChrono":"0000","libelleEntreprise":{"denomination":"TRANSACTION @ CONSEIL","denominationEirl":null,"enseigne":null,"nomCommercial":null,"sigle":null},"siren":502363229,"nic":"00019","adresse":{"lignes":["15 ROUTE DES ECOLES "],"codePostal":"31130","bureauDistributeur":"PIN-BALMA"},"codePaysRegistreEtranger":null,"greffe":{"numero":"3102","nom":"TOULOUSE","codeGroupement":"51","codeEDI":"G3102","typeTribunalReel":"TC","nomGreffeMin":null,"codeLiasseDGFIP":null},"typeEtab":"SIE","produitAuPanier":"AJOUTABLE","typeInscription":1,"sourceDonnees":"GTC","radie":false,"dateRadiation":null,"nbEtablissements":1,"activite":{"codeNAF":"7022Z","libelleNAF":"Conseil pour les affaires et autres conseils de gestion"},"etatSurveillance":"SURVEILLABLE"}],"typeProduitMisEnAvant":"EXTRAIT","critereRecherchePrincipal":null,"entrepRechInfosComplementaires":null}'''
dic = json.loads(content)
# print(dic)
items = dic['items']
for item in items:
    id = item['id']  # 公司id
    numeroDossier = item['numeroDossier']  # 编号
    name = item['libelleEntreprise']['denomination']  # 公司名称
    siren = item['siren']
    nic = item['nic']
    siret = str(siren) + str(nic)
    lignes = item['adresse']['lignes']
    codePostal = item['adresse']['codePostal'] # 邮编
    city = item['adresse']['bureauDistributeur']
    address = ''.join(lignes) + city
    sourceDonnees = item['sourceDonnees']
    typeEtab = item['typeEtab'] # 启动类型
    activite = item['activite']
    NAF = item['activite']['codeNAF'] # 法国产业分类代码
    NAFName = item['activite']['libelleNAF']
    status = item['etatSurveillance']
    print(id,numeroDossier,name,siren,nic,siren,lignes,codePostal,city,address,sourceDonnees,typeEtab,activite,NAF,NAFName,status)
    # 将法语转成英文
    translationTable = str.maketrans("éàèùâêîôûç", "eaeuaeiouc")
    t = name.translate(translationTable).lower()
    res = re.sub(r'\W', '-', t)
    detail_url = f'https://www.infogreffe.com/entreprise-societe/{siren}-{res}-{numeroDossier}0000.html?typeProduitOnglet=EXTRAIT&afficherretour=true'
